package edu.yctc.weblog.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 
 * @className ServiceAspect
 * @description service切面, 主要用于捕获异常
 * @author zyj
 * @date 2020年6月16日
 *
 */
@Component
@Aspect
public class ServiceAspect {

    private final static Logger LOG = LoggerFactory.getLogger("serviceLogger");

    @Pointcut("execution(* edu.yctc.weblog.service.impl.*.*(..))")
    public void serviceLog() {};

    @Around("serviceLog()")
    public Object printServiceLog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object result = null;
        // 类名
        String className = proceedingJoinPoint.getTarget().getClass().getName();
        // 方法名
        String method = proceedingJoinPoint.getSignature().getName();
        // 参数列表
        Object[] args = proceedingJoinPoint.getArgs();
        try {
            result = proceedingJoinPoint.proceed();
            LOG.info("method={}, args={}", className + "." + method, args);
        } catch (Exception e) {
            LOG.error("system error, method={}, args={}", className + "." + method, args, e);
        }
        return result;
    }

}
